﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using IAM.Interface.Model;

namespace EVM.Implementation
{
    public class Business
    {
        internal ACMMode GetACIModel()
        {
            Wicresoft.Logger.Log.Debug("IAM", "Begin -> ACIMgntImplement::GetACIModel");

            ACSections sections = new ACSections();
            ACSection section = new ACSection();
            section.ID = "Role";
            sections.Items.Add(section);
            Dimensions dimensions = new Dimensions();
            Dimension dimension = new Dimension();
            dimension.ID = "Role";
            dimension.Display = "Role";
            dimension.Hierachical = false;
            dimension.MultiCheck = true;
            dimension.ACSection = section;
            dimension.DimensionLevels = new DimensionLevels();
            DimensionLevel level = new DimensionLevel();
            level.ID = "";
            level.Display = "";
            dimension.DimensionLevels.Items.Add(level);
            dimension.ACItems = new ACItems();
            foreach (var role in IAMHelper.GetRoleList())
            {
                ACItem item = new ACItem();
                item.DimensionLevel = level;
                item.ID = role;
                item.Display = role;
                item.Parent = null;
                dimension.ACItems.Items.Add(item);
            }

            dimensions.Items.Add(dimension);

            ACMMode mode = new ACMMode();
            mode.MultiSection = false;
            mode.SectionList = sections;
            mode.DimensionList = dimensions;

            Wicresoft.Logger.Log.Debug("IAM", "End -> ACIMgntImplement::GetACIModel");

            return mode;
        }

        internal ACI GetUserACI(AccountInfo accountinfo)
        {
            Wicresoft.Logger.Log.Debug("IAM", "Begin -> ACIMgntImplement::GetUserACI");

            ACI aci = new ACI();

            aci.AccountInfo = accountinfo;
            aci.ACISessions = new ACISessions();

            foreach (var role in IAMHelper.GetRolesByUser(accountinfo.AppAccount))
            {
                ACISession session = new ACISession();
                CheckedItem item = new CheckedItem();
                item.ACItemID = role;
                item.Value = true;
                session.Items.Add(item);
                aci.ACISessions.Items.Add(session);
            }

            Wicresoft.Logger.Log.Debug("IAM", "End -> ACIMgntImplement::GetUserACI");
            return aci;
        }

        internal ACI SetUserACI(ACI aci)
        {
            Wicresoft.Logger.Log.Debug("IAM", "Begin -> ACIMgntImplement::SetUserACI");

            List<string> roles = new List<string>();

            foreach (ACISession session in aci.ACISessions.Items)
                foreach (CheckedItem item in session.Items)
                    roles.Add(item.ACItemID);

            IAMHelper.AddRolesToUser(aci.AccountInfo.AppAccount, roles);

            Wicresoft.Logger.Log.Debug("IAM", "End -> ACIMgntImplement::SetUserACI");
            return aci;
        }

        internal AIDelegations GetDelegationInfo(AccountInfo accountinfo)
        {
            throw new NotImplementedException();
        }

        internal Modules GetModuleList()
        {
            throw new NotImplementedException();
        }

        internal AIDelegations SetDelegationInfo(AIDelegations aidelegations)
        {
            throw new NotImplementedException();
        }

        internal AccountInfo CreateIdentity(Identity xmlrequest)
        {
            xmlrequest.Accountinfo.AppAccount = xmlrequest.Accountinfo.IAMID;
            return xmlrequest.Accountinfo;
        }

        internal AccountInfo DeleteIdentity(Identity xmlrequest)
        {
            throw new NotImplementedException();
        }

        internal AccountInfo DisableIdentity(Identity xmlrequest)
        {
            IAMHelper.AddRolesToUser(xmlrequest.Accountinfo.AppAccount, new string[] { });
            return xmlrequest.Accountinfo;
        }

        internal AccountInfo EnableIdentity(Identity xmlrequest)
        {
            throw new NotImplementedException();
        }

        internal AccountInfo UpdateIdentity(Identity xmlrequest)
        {
            throw new NotImplementedException();
        }

        internal bool CreateOrganize(OrganizeInfo organizeinfo)
        {
            throw new NotImplementedException();
        }

        internal bool DeleteOrganize(OrganizeDeleteParameter organizedeleteparameter)
        {
            throw new NotImplementedException();
        }

        internal bool UpdateOrganize(OrganizeInfo organizeinfo)
        {
            throw new NotImplementedException();
        }



        public string GenerateAllUserACI()
        {
            Wicresoft.Logger.Log.Debug("IAM", "Begin -> ACIMgntImplement::GenerateAllUserACI");

            var userList = IAMHelper.GetAllUsers();
            UserACIs aciList = new UserACIs();
            foreach (var user in userList)
            {
                UserACI aci = GetSingleUserACI(user);
                if (aci != null)
                {
                    aciList.Items.Add(aci);
                }
            }
            string xML = aciList.XML;
            string str2 = "<ExportDate ExportTime=\"\" AppName=\"EVM\" DataType=\"IDEN_USERLIST\" ><Result ErrorCode=\"0\" ErrorMessage=\"OK\" > </Result>";
            string str3 = "</ExportDate>";

            Wicresoft.Logger.Log.Debug("IAM", "End -> ACIMgntImplement::GenerateAllUserACI");
            return (str2 + xML + str3);
        }

        private UserACI GetSingleUserACI(dynamic user)
        {
            UserACI aci = new UserACI();

            aci.AccountInfo = new AccountInfo();
            aci.AccountInfo.HRISEID = string.Empty;
            aci.AccountInfo.CSLAccount = string.Empty;
            aci.AccountInfo.CILX500 = user.LoginName; 
            aci.AccountInfo.AppAccount = user.EmployeeId;
            aci.AccountInfo.ADAccount = string.Empty;
            aci.AccountInfo.UPI = string.Empty;
            aci.AccountInfo.IAMID = user.EmployeeId;

            aci.ACISessions = new ACISessions();


            foreach (string role in user.Roles)
            {
                ACISession session = new ACISession();
                CheckedItem item = new CheckedItem();

                item.ACItemID = role;
                item.Value = true;

                session.Items.Add(item);
                aci.ACISessions.Items.Add(session);
            }

            return aci;
        }
    }
}